<?php

namespace app\admin\command\Task;

use app\admin\command\Api\library\Builder;
use app\admin\model\ReportDaily;
use app\admin\model\ReportMonth;
use app\admin\model\ReportYear;
use app\admin\model\StundentVipLogs;
use app\admin\model\User;
use app\admin\model\VipConfig;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;

class Report extends Command
{
    //每日统计 00：00执行
    protected function configure()
    {
        $this->setName('report:daily')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        //查询当前学校数量
        $school_count = \app\admin\model\School::count();
        //查询当前年级数量
        $grade_count = \app\admin\model\Grade::count();
        //查询当前班级数量
        $class_count = \app\admin\model\GradeClass::count();
        //查询当前学生数量
        $student_count = (new User())->where("type",2)->count();
        //当前学生会员数量
        $student_vip_count = (new User())->where("type",2)->where("vip_type",">",0)->count();
        //会员总金额
        $vip_amount = (new  StundentVipLogs())->where("operate_type","add")->sum("amount");
        //获取会员具体类型数据
        $vipConfig = (new VipConfig())->select();
        $student_vip_1_count = 0;
        $student_vip_2_count = 0;
        $student_vip_3_count = 0;
        $vip_1_amount = 0;
        $vip_2_amount = 0;
        $vip_3_amount = 0;
        foreach ($vipConfig as $key => $value){
            if ($value["vip_year"] == 1) {
                $student_vip_1_count = (new User())->where("type",2)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_1_amount = (new  StundentVipLogs())->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");
            }elseif ($value["vip_year"] == 2){
                $student_vip_2_count = (new User())->where("type",2)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_2_amount = (new  StundentVipLogs())->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");

            }elseif ($value["vip_year"] == 3){
                $student_vip_3_count = (new User())->where("type",2)->where("vip_type",$value['id'])->count();
                //会员总金额
                $vip_3_amount = (new  StundentVipLogs())->where("operate_type","add")->where("vip_type",$value['id'])->sum("amount");
            }
        }

        //
        //获取时间减一天
        $date = date("Y-m-d",strtotime("-1 day"));
        $month = date("Y-m",strtotime("-1 day"));
        $year = date("Y",strtotime("-1 day"));
        //插入数据
        $report_daily = (new ReportDaily())->where("date",$date)->find();
        if ($report_daily){
            (new ReportDaily())->where("id",$report_daily->id)->update(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportDaily())->insert(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }


        //月份的进行更新
        $report_month = (new ReportMonth())->where("month",$month)->find();
        if ($report_month){
            (new ReportMonth())->where("id",$report_month->id)->update(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportMonth())->insert(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }

        //年统计进行更新
        $report_year = (new ReportYear())->where("year",$year)->find();
        if ($report_year){
            (new ReportYear())->where("id",$report_year->id)->update(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "update_time" => time(),
                ]
            );
        }else{
            (new ReportYear())->insert(
                [
                    "school_nums" => $school_count,
                    "grade_nums" => $grade_count,
                    "class_nums" => $class_count,
                    "user_nums" => $student_count,
                    "vip_nums" => $student_vip_count,
                    "vip_amt" => $student_vip_count,
                    "vip_1_nums" => $student_vip_1_count,
                    "vip_1_amt" => $vip_1_amount,
                    "vip_2_nums" => $student_vip_2_count,
                    "vip_2_amt" => $vip_2_amount,
                    "vip_3_nums" => $student_vip_3_count,
                    "vip_3_amt" => $vip_3_amount,
                    "year" => $year,
                    "month" => $month,
                    "date" =>$date,
                    "create_time" => time(),
                    "update_time" => time(),
                ]
            );
        }

        echo "success";
    }


}
